<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>订单管理</title>
    <link href="/static/libs/bootstrap.css" rel="stylesheet">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
        .navbar {
            background-color: #ff5000;
            color: white;
            padding: 10px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .navbar .logo {
            display: flex;
            align-items: center;
        }
        .navbar .logo span {
            margin-left: 10px;
        }
        .navbar a {
            color: white;
            text-decoration: none;
            margin: 0 10px;
        }
        .search-bar {
            display: flex;
            justify-content: center;
            margin: 20px 0;
        }
        .search-bar input {
            width: 500px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px 0 0 5px;
        }
        .search-bar button {
            padding: 10px;
            background-color: #ff5000;
            border: none;
            color: white;
            border-radius: 0 5px 5px 0;
        }
        .category-menu {
            background-color: #f5f5f5;
            padding: 10px;
            display: flex;
            justify-content: space-around;
        }
        .category-menu a {
            text-decoration: none;
            color: #333;
            padding: 10px 20px;
        }
        .content-box {
            padding: 20px;
        }
        .left-nav, .right-content {
            float: left;
        }
        .left-nav {
            width: 20%;
            background-color: #f5f5f5;
            padding: 10px;
        }
        .left-nav a {
            display: block;
            text-decoration: none;
            color: #333;
            padding: 10px 20px;
            margin: 5px 0;
            background-color: #fff;
            border-radius: 5px;
        }
        .left-nav a.active, .left-nav a:hover {
            background-color: #ff5000;
            color: white;
        }
        .right-content {
            width: 75%;
            margin-left: 5%;
        }
        .right-title {
            font-size: 24px;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input, .form-group select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        .btn-submit {
            padding: 10px 20px;
            background-color: #ff5000;
            border: none;
            color: white;
            border-radius: 5px;
            cursor: pointer;
        }
        .btn-submit:hover {
            background-color: #e04c00;
        }
        .address-list {
            margin-top: 30px;
        }
        .address-list table {
            width: 100%;
            border-collapse: collapse;
        }
        .address-list th, .address-list td {
            padding: 10px;
            border: 1px solid #ccc;
            text-align: left;
        }
        .address-list th {
            background-color: #f5f5f5;
        }
    </style>
    <style>
        .info-item {
            display: flex;
            margin: 10px 0;
        }
        .info-item .label {
            font-weight: bold;
            width: 100px;
        }
    </style>
</head>
<body>
<div class="navbar">
    <div class="logo">机械工业</div>
    <div class="nav-links">
        <a id="index1Link" href="#">首页</a>
    </div>
</div>

<div class="search-bar">
    <input type="text" placeholder="搜索商品、店铺">
    <button>搜索</button>
</div>

<div class="content-box clear_fix">
    <div class="left-nav">
        <a id="userMessageLink" href="#">收货地址管理</a>
        <a id="orderlistLink" href="#">我的订单</a>
        <a id="cartLink" href="#">我的购物车</a>
        <a id="information_modificationLink" href="#">个人资料</a>
        <a id="password_modificationLink" href="#">修改密码</a>
    </div>

    <div class="content" style="display: flex;flex-direction: column">
        <table class="table">
            <thead>
            <tr>
                <th scope="col">订单编号</th>
                <th scope="col">用户编号</th>
                <th scope="col">订单付款金额</th>
                <th scope="col">付款类型</th>
                <th scope="col">运费</th>
                <th scope="col">订单状态</th>
                <th scope="col">操作</th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>

    <div class="modal" id="order-modal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="modal-title">订单详情</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="info-item">
                        <div class="label">收货方式</div>
                        <div class="val">收货联系方式: <span id="address"></span></div>
                    </div>
                    <div class="info-item">
                        <div class="label">订单编号: </div>
                        <div class="val"><span id="orderNo"></span></div>
                    </div>
                    <div class="info-item">
                        <div class="label">下单时间: </div>
                        <div class="val"><span id="created"></span></div>
                    </div>
                    <div class="info-item">
                        <div class="label">应支付金额: </div>
                        <div class="val">￥<span id="amount"></span></div>
                    </div>
                    <div class="info-item">
                        <div class="label">支付方式: </div>
                        <div class="val" id="type"></div>
                    </div>
                    <div class="info-item">
                        <div class="label">订单状态</div>
                        <div class="val" id="status"></div>
                    </div>
                    <div class="info-item">
                        <div class="label">支付时间</div>
                        <div class="val"><span id="paymentTime"></span></div>
                    </div>
                    <hr>
                    <h4>商品信息</h4>
                    <div class="info-item">
                        <div class="label">商品名</div>
                        <div class="val"><span id="goodsName"></span></div>
                    </div>
                    <div class="info-item">
                        <div class="label">商品单价</div>
                        <div class="val">￥<span id="price"></span></div>
                    </div>
                    <div class="info-item">
                        <div class="label">商品数量</div>
                        <div class="val"><span id="quantity"></span></div>
                    </div>
                    <div class="info-item">
                        <div class="label">总价</div>
                        <div class="val">￥<span id="totalPrice"></span></div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" style="display: none;" class="btn btn-primary" id="pay">支付</button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>


</div>
</body>
<script src="/static/libs/bootstrap.js"></script>
<script src="/static/js/axios.js"></script>

<script>


    function getUserIdFromUrl() {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get('id');
    }
    const userId = getUserIdFromUrl();
    const userMessageLink = document.getElementById("userMessageLink");
    const index1Link = document.getElementById("index1Link");
    const cartLink = document.getElementById("cartLink");
    const orderlistLink = document.getElementById("orderlistLink");
    const information_modificationLink = document.getElementById("information_modificationLink");
    const password_modificationLink = document.getElementById("password_modificationLink");
    userMessageLink.href = `userMessage?id=${userId}`;
    index1Link.href = `index1?id=${userId}`;
    cartLink.href =`cart?id=${userId}`;
    orderlistLink.href = `orderlist?id=${userId}`;
    information_modificationLink.href = `information_modification?id=${userId}`;
    password_modificationLink.href = ` password_modification?id=${userId}`;
    const orderModal = new bootstrap.Modal(document.getElementById("order-modal"));
    const detail = id => {
        orderModal.show();
        axios.get("http://localhost:8080/orders/detail?id=" + id).then(res => {
            document.querySelector("#address").innerText = res.data.address.province + "-" + res.data.address.city + "-" + res.data.address.district + " " + res.data.address.zip + " " + res.data.address.phone;
            document.querySelector("#orderNo").innerText = res.data.orderNo;
            document.querySelector("#created").innerText = res.data.created;
            document.querySelector("#amount").innerText = res.data.amount;
            document.querySelector("#type").innerText = res.data.type === 1 ? "在线支付" : "货到付款";
            document.querySelector("#status").innerText = res.data.status === 1 ? "待付款" : (res.data.status === 2 ? "已付款" : (res
                .data.status === 3 ? "已发货" : (res.data.status === 4 ? "已取消" : "")));
            document.querySelector("#paymentTime").innerText = res.data.paymentTime;
            document.querySelector("#goodsName").innerText = res.data.orderList.goodsName;
            document.querySelector("#price").innerText = res.data.orderList.price;
            document.querySelector("#quantity").innerText = res.data.orderList.quantity;
            document.querySelector("#totalPrice").innerText = res.data.orderList.totalPrice;

            if (res.data.status === 1) {
                document.querySelector("#pay").style.display = "block";
            } else {
                document.querySelector("#pay").style.display = "none";
            }
        })
    }
    const remove = id => {
        axios.delete("http://localhost:8080/orders/" + Number.parseInt(id)).then(res => {
            window.location.reload();
        });
    }

    onload = () => {
        axios.get("http://localhost:8080/orders/order/"+userId).then(res => {
            res.data.forEach(item => {
                let template = `
        <tr>
            <th scope="row">{{orderNo}}</th>
            <td>{{uid}}</td>
            <td>{{amount}}</td>
            <td class="type"></td>
            <td>{{freight}}</td>
            <td class="status"></td>
            <td>
                <div class="btn btn-warning" onclick="detail({{orderNo}})">查看详情</div>
                <div class="btn btn-danger" onclick="remove({{id}})">删除</div>
            </td>
        </tr>
        `
                template = template.replaceAll("{{orderNo}}", item.orderNo);
                template = template.replace("{{uid}}", item.uid);
                template = template.replace("{{amount}}", "￥" + item.amount);
                template = template.replace("{{freight}}", item.freight);
                template = template.replace("{{id}}", item.id);
                template = template.replace("{{status}}", item.status);
                let tr = document.createElement("tr");
                tr.innerHTML = template;
                tr.querySelector(".type").innerText = item.type === 1 ? "在线支付" : "货到付款";
                switch (item.status) {
                    case 1:
                        tr.querySelector(".status").innerText = "待付款";
                        break;
                    case 2:
                        tr.querySelector(".status").innerText = "已付款";
                        break;
                    case 3:
                        tr.querySelector(".status").innerText = "已发货";
                        break;
                    case 4:
                        tr.querySelector(".status").innerText = "已取消";
                        break;
                }
                document.querySelector(".table tbody").appendChild(tr);
            });
        })

    }
    document.querySelector("#pay").addEventListener("click", function() {
        const orderId = document.querySelector("#orderNo").innerText;
        const price = document.querySelector("#amount").innerText;
        const title = document.querySelector("#goodsName").innerText;
        redirectToPayment(orderId, price, title);
    });

    function redirectToPayment(orderId, price, title) {
        const url = `/payment?orderid=${encodeURIComponent(orderId)}&price=${encodeURIComponent(price)}&title=${encodeURIComponent(title)}`;
        window.location.href = url;
    }
</script>


</html>
